package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.DepartmentInfo;
import com.example.prison.service.DepartmentInfoService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("部门管理")
@RestController
@RequestMapping("/api")
public class DepartmentInfoController {

    @Autowired
    private DepartmentInfoService departmentInfoService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取部门列表")
    @GetMapping("/getDepartment")
    public Response<PageInfo<DepartmentInfo>> getDepartment(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取部门列表");
        PageInfo<DepartmentInfo> pageInfo = departmentInfoService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增部门")
    @PostMapping("/addDepartment")
    public Response<?> addDepartment(@RequestBody DepartmentInfo department) {
        log.info("新增部门");
        systemLogService.add("新增部门");
        int result = departmentInfoService.add(department);
        if (result == -1) {
            return Response.error("部门单位已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("删除部门")
    @PostMapping("/deleteDepartment")
    public Response<?> deleteDepartment(@Parameter(description = "部门单位ID") @RequestBody Integer[] ids) {
        log.info("删除部门");
        systemLogService.add("删除部门");
        departmentInfoService.delete(ids);
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑部门")
    @PostMapping("/updateDepartment")
    public Response<?> updateDepartment(@RequestBody DepartmentInfo department) {
        log.info("编辑部门");
        systemLogService.add("编辑部门");
        Integer id = department.getDepartmentId();
        int result = departmentInfoService.update(department, id);
        if (result == -1) {
            return Response.error("更新失败！部门单位名已存在！");
        } else {
            return Response.success("更新成功！");
        }
    }

    @ApiOperation("依据id获取部门单位列表")
    @GetMapping("/getDepartmentById")
    public Response<DepartmentInfo> getDepartmentById(@Parameter(description = "部门单位ID") @RequestParam(value = "departmentId") Integer departmentId) {
        log.info("依据id获取部门单位列表");
        systemLogService.add("依据id获取部门单位列表");
        DepartmentInfo department = departmentInfoService.getById(departmentId);
        if(ObjectUtils.isEmpty(department)) {
            return Response.error("此部门单位id不存在！");
        }
        return Response.success("获取成功！", department);
    }

    @ApiOperation("依据名称获取部门单位")
    @GetMapping("/getDepartmentByName")
    public Response<PageInfo<DepartmentInfo>> getDepartmentByName(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @Parameter(description = "部门单位名称")@RequestParam(value = "departmentName") String departmentName) {
        log.info("依据名称获取部门单位");
        systemLogService.add("依据名称获取部门单位");
        PageInfo<DepartmentInfo> pageInfo = departmentInfoService.getByName(pageNumber, pageSize, departmentName);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此部门单位名不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
